<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <style type="text/css">

    </style>

</head>
<body>
<div class="page-return">
    <div class="ops-form">
        <div class="page-head">
            <form class="form-horizontal tasi-form" method="post" id="params" action="">
                <div class="form-group">
                    <label class="col-sm-1 control-label">流程Key</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="key" id="key" value="">
                    </div>
                    <div class="col-sm-3">
                        <button type="button" class="btn btn-info" onclick="" id="processSearch">搜索</button>
                        <button type="button" class="btn btn-info" onclick="" id="processInPut">导入流程</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="ops-table" >
        <caption>
            <div class="ops-edit">
                <button type="button" class="btn btn-xs btn-danger" id="processDeleteBtn"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>
            </div>
        </caption>
        <div class="ops-table-content">
            <table id="processTable"></table>
        </div>
    </div>
</div>
</body>
<script>
    $(function(){
        var processTable = $("#processTable").bootstrapTable({ // 对应table标签的id
            url: "/workflow/process/list", // 获取表格数据的url
            queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                var paramOption = {
                    pageSize: params.limit, // 每页要显示的数据条数
                    pageNumber: (params.offset/params.limit)+1, // 每页显示数据的开始行号
                    sortName: params.sort, // 要排序的字段
                    searchText:'',//搜索内容
                    sortOrder: params.order, // 排序规则
                    dataId: $("#dataId").val() // 额外添加的参数
                }
                return paramOption;
            },
            columns: [
                {
                    checkbox: true // 显示一个勾选框
                },{
                    field: 'name',
                    title: '流程名称',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },
                {
                    field: 'key',
                    title: '流程定义key',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },
                {
                    field: 'category',
                    title: '分类',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },
                {
                    field: 'version',
                    title: '版本',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return "v"+value;
                    }
                },
                {
                    field: 'deploymentTime',
                    title: '部署时间',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },
                {
                    field: 'resourceName',
                    title: '流程bpmn文件名称',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return '<a href="javascript:void(0)" onclick="goViewXml(\'' + row.deploymentId + '\')">'+row.resourceName+'</a>';
                    }
                },
                {
                    field: 'diagramResourceName',
                    title: '流程图片名称',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return '<a href="javascript:void(0)" onclick="goViewPng(\'' + row.deploymentId + '\')">'+row.diagramResourceName+'</a>';
                    }
                },
                {
                    field: 'isSuspended',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index){ // 单元格格式化函数
                        var text = '';
                        if (row.isSuspended) {
                            text = '<h7 style="color:red">已挂起</h7>';
                        }else{
                            text = '<h7 style="color:green">已激活</h7><img src="public/images/canvas/runing.gif" width="12px;" />';
                        }
                        return text;
                    }
                },
                {
                    title: "操作",
                    width: 190, // 定义列的宽度，单位为像素px
                    formatter: function (value, row, index) {
                        if (row.isSuspended){
                            return '<button class="btn btn-xs btn-info" onclick="addModel(\'' + row.id + '\')">映射模型</button> &nbsp;'
                                +'<button class="btn btn-xs btn-success" onclick="exportZip(\'' + row.deploymentId + '\')"><i class="ace-icon fa fa-cloud-download bigger-120" title="打包下载"></i></button> &nbsp;'
                                +'<button class="btn btn-xs btn-warning" onclick="onoff(\'' + row.id + '\',\'1\')"><i class="ace-icon glyphicon glyphicon-play" title="激活"></i></button> &nbsp;'
                                +'<button class="btn btn-xs btn-danger" onclick="deldefined(\'' + row.deploymentId + '\')"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>   ';
                        }else{
                            return '<button class="btn btn-xs btn-info" onclick="addModel(\'' + row.id + '\')">映射模型</button> &nbsp;'
                                +'<button class="btn btn-xs btn-success" onclick="exportZip(\'' + row.deploymentId + '\')"><i class="ace-icon fa fa-cloud-download bigger-120" title="打包下载"></i></button> &nbsp;'
                                +'<button class="btn btn-xs btn-warning" onclick="onoff(\'' + row.id + '\',\'2\')"><i class="ace-icon glyphicon glyphicon-off" title="挂起"></i></button> &nbsp;'
                                +'<button class="btn btn-xs btn-danger" onclick="deldefined(\'' + row.deploymentId + '\')"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>   ';
                        }
                    }
                }
            ]
        });

        /**
         *  删除按钮
         */
        $("#processDeleteBtn").click(function () {
            var objArray = processTable.bootstrapTable('getSelections');
            if(objArray.length < 1){
                layer.msg('请选择至少一条记录进行删除', function(){});
                return false;
            }
            var idArray = [];
            for (i = 0; i < objArray.length; i++) {
                idArray[i] = objArray[i].deploymentId;
            }
            layer.confirm('删除流程，不管流程是否在启动状态都会删除！<br/>是否确定删除？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type:"get",
                    url:'/workflow/process/delete/?idArray='+idArray.join(','),
                    dataType:"json",
                    success:function(data){
                        if(data.resCode == '200'){
                            parent.$("#processTable").bootstrapTable('refresh',{silent: true});
                            parent.layer.msg(data.resMessage,{time:1200});
                            return true;
                        }else {
                            parent.layer.msg(data.resMessage);
                            return false;
                        }
                    },
                    error:function(data){
                        parent.layer.msg("错误："+data.resMessage);
                    }
                })
            }, function(){
                layer.msg('取消操作', {
                    time: 1500 //1.5s后自动关闭
                });
            });
        });
    })

    /**
     * 删除
     */
    function deldefined(Id){
        layer.confirm('删除流程，不管流程是否在启动状态都会删除！<br/>是否确定删除？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var idArray = [];
            idArray[0] = Id;
            $.ajax({
                type:"get",
                url:'/workflow/process/delete/?idArray='+idArray.join(','),
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#processTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 流程的挂起和激活，流程挂起后无法继续任务，流程激活后可以执行任务
     * @param definedId 流程定义id
     * @param status 状态 1代表激活，2，代表挂起
     */
    function onoff(definedId,status) {
        var str=(status==1?"激活":"挂起");
        layer.confirm('流程挂起后,任务无法继续，直到流程再次被激活！<br> 确认'+str+'?', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:"POST",
                url:'/workflow/process/onoff',
                data: {id:definedId,isSuspended:status},
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#processTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 导出资源文件
     * @param deploymentId 部署Id
     */
    function exportZip(deploymentId) {
        window.location.href="<%=basePath%>/workflow/process/download/?deploymentId="+deploymentId;
    }

    /**
     * 映射为模型
     * @param processDefinedId 模型id
     */
    function addModel(processDefinedId) {
        layer.confirm('是否确定将流程映射出一个新的模型？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type:"POST",
                url:'/workflow/process/addmodel',
                data: {id:processDefinedId},
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#processTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 预览Xml图片
     * @param deploymentId 部署Id
     */
    function goViewXml(deploymentId){
        layer.open({
            type: 2,
            title: '预览xml',
            shadeClose: true,
            shade: false,
            area: ['1000px', '608px'],
            content: '/workflow/process/viewxml/?deploymentId='+deploymentId
        });
    }

    /**
     * 预览png图片
     * @param deploymentId
     */
    function goViewPng(deploymentId){
        layer.open({
            type: 2,
            title: '预览流程图片',
            shadeClose: true,
            shade: false,
            maxmin: true,
            area: ['1000px', '408px'],
            content: '/workflow/process/viewpng/?deploymentId='+deploymentId
        });
    }

    /**
     *  搜索按钮
     */
    $("#processSearch").click(function () {
        $("#processTable").bootstrapTable('refresh',{
            silent: true,
            query: {
                key:$("#key").val(),
            }
        });
    });
    /**
     * 导入流程，一般适合外部系统的工作流程导入本系统
     */
    $("#processInPut").click(function () {
        layer.open({
            type: 2,
            title: '导入流程',
            shadeClose: true,
            shade: false,
            area: ['300px', '190px'],
            content: '/workflow/process/uploadprocess'
        });
    });
</script>
</html>